//
//  Person.m
//  OC07_内存管理
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "Person.h"

@implementation Person
@synthesize name = _name;
@synthesize sex = _sex;
@synthesize age =_age;
@synthesize workStation = _workStation;
-(void)dealloc{
    
    NSLog(@"内存空间被释放.");
    //内存真正被释放是调用父类的dealloc方法.
    //在ARC模式下不可以调用父类的dealloc方法.
    
    //在释放Person前先释放对象属性指向的空间.
    [_name release];
    [_sex release];
    [_workStation release];
    
    [super dealloc];
    
    
}


//retain的语义特性.
-(void)setName:(NSString *)name{
    if(_name != name){
        [_name release];
        _name = [name retain];
    }
}

-(NSString *)name{
    
    return [[_name retain] autorelease];
}
//copy语义特性
-(void)setSex:(NSString *)sex{
    if(_sex != sex){
        [_sex release];
        _sex = [sex copy];//对象必须遵循NSCoping协议.
    }
}

-(NSString *)sex{
    return [[_sex retain] autorelease];
}

//assign语义特性
-(void)setAge:(NSInteger)age{
    _age = age;
}

-(NSInteger)age{
    return _age;
}
//retain
-(void)setWorkStation:(NSString *)workStation{
    [_workStation release];
    _workStation = [workStation retain];
}

-(NSString *)workStation{
    return [[_workStation retain] autorelease];
}
//自定义初始化内部实现
-(instancetype)initWithName:(NSString *)name withSex:(NSString *)sex withAge:(NSInteger)age withWorkStation:(NSString *)workStation{
    self = [super init];
    if (self) {
        //第一种写法:
//        _name = [name retain];
//        _sex = [sex copy];
//        _workStation =[workStation retain];
        _age = age;
        
        
        //第二种写法:(常用写法)
        self.name = name;
        self.sex = sex;
    }
    return self;
}
//遍历构造器的内部实现
+(instancetype)personWithName:(NSString *)name withSex:(NSString *)sex withAge:(NSInteger)age withWorkStation:(NSString *)workStation{
    Person *person = [[Person alloc] initWithName:name withSex:sex withAge:age withWorkStation:workStation];
    return [person autorelease];
}

//NSCoping协议方法的实现

//深拷贝的实现.
-(id)copyWithZone:(NSZone *)zone{
    Person *per = [[Person allocWithZone:zone] init];
    per.name = [_name mutableCopy];
    per.sex = [_sex mutableCopy];
    per.age = _age;
    
    return per;
}

//浅拷贝的实现
//-(id)copyWithZone:(NSZone *)zone{
//    Person *per = [[Person allocWithZone:zone] init];
//    
//    per.name = self.name;
//    per.sex = self.sex;
//    per.age = self.age;
//    per.workStation = self.workStation;
//    return per;
//}




@end
